import md5 from '@/utils/blueimp-md5.js'
export default {
  data() {
    return {
      firsttime: true, //第一次设置密码
      passwordPlaceholder: '请输入密码', //input默认提示
      showpasswordPlaceholder: false, //密码小标题
      showPasswordClearIcon: false, //密码小叉叉
      showPassword: true, //隐藏显示密码
      Password: '', //input输入的值
      isDisabled: true, //获取验证码
      show: false, //弹窗
      eye: 'eye-off', //显示隐藏的眼睛图标
      oldUser: '', //老用户
      UserName: '', //用户名
      showPopup: false, //弹窗
      isAgree: true, //勾选免责

      passwordPlaceholderagen: '再次输入密码', //input默认提示
      Passwordagen: '', //input输入的值
      showpasswordPlaceholderagen: false, //密码小标题
      showPasswordClearIconagen: false, //密码小叉叉
      showPasswordagen: true, //隐藏显示密码

      guidePages: false, //隐藏显示设置密码
      newusser: false, //true新用户，false老用户
    }
  },
  onLoad(e) {
    this.newusser = JSON.parse(e.type)
    this.loadExecution() //调用设置密码页面
    this.UserName = e.UserName
    uni.loadFontFace({
      family: 'PingFang SC',
      source: 'url("https://ztcbs.yzsheng.com/static/font/PingFang SC Medium.ttf")',
    })

  },

  methods: {
    md5,
    //第一次设置密码de下一步
    fistgoit() {
      this.firsttime = false //第一次输入密码成功
    },
    changeInputf(e) {
      this.Password = e
    },
    finish(e) {
      var reg = /[\u4E00-\u9FA5]/g
      if (reg.test(e)) {
        this.Password = ''
        this.$refs.uToast.show({
          title: '不能输入汉字!',
          icon: false,
          type: 'warning'
        })
      } else {
        if (e.length === 6) { //验证码输入完毕，并且验证码是正确返回的
          this.Password = e
          this.isDisabled = false
        }
      }
    },
    //第二次密码输入
    changeInputss(e) {
      this.Passwordagen = e
    },
    finishcss(e) {
      var reg = /[\u4E00-\u9FA5]/g
      if (reg.test(e)) {
        this.Passwordagen = ''
        this.$refs.uToast.show({
          title: '不能输入汉字!',
          icon: false,
          type: 'warning'
        })
      } else {
        if (e.length === 6) { //验证码输入完毕，并且验证码是正确返回的
          this.Passwordagen = e
          this.isDisabled = false
        }
      }
    },
    loadExecution: function() {
      try {
        if (this.newusser) {
          this.guidePages = true
        } else {
          // 跳过密码设置,进入选择需求
          uni.reLaunch({
            url: '/pages_commodity/selectRequirements/selectRequirements?type=' + this.newusser
          })
        }
      } catch (e) {
        uni.setStorage({
          key: 'passwordlogin',
          data: true,
          success: function() {
            console.log('error时存储passwordlogin')
          }
        })
        this.guidePages = true
      }
      return
      // 跳过密码设置,进入选择需求
      uni.reLaunch({
        url: '/pages_commodity/selectRequirements/selectRequirements?type=' + this.newusser
      })
    },
    // 先看看
    skipindex() {
      uni.setStorage({
        key: 'launchFlag',
        data: true,
      })
      // 跳过密码设置,进入选择需求
      uni.reLaunch({
        url: '/pages_commodity/selectRequirements/selectRequirements?type=' + this.newusser
      })
    },
    //点击下一步密码登录
    async login() {
      //判断两次密码输入是否相同
      if (this.Password !== this.Passwordagen) {
        uni.showToast({
          title: '两次密码不相同，请重新输入',
          duration: 1000,
          icon: 'none'
        })
        this.Passwordagen = ''
        return false
      }
      const md5Pwd = this.md5(this.Password)
      this.SetMyPassword(md5Pwd)
    },
    async SetMyPassword(md5Pwd) {
      try {
        const res = await this.$api.user.SetMyPassword({
          password: md5Pwd,
          secondPassword: md5Pwd
        })
        if (res.code === 200) {
          //如果不是新用户直接跳转首页，否则进入我的车单页面
          if (this.newusser) {
            uni.reLaunch({
              url: '/pages_commodity/selectRequirements/selectRequirements?type=' + this.newusser
            })
          } else {
            uni.switchTab({
              url: '/pages/index/index'
            })
          }
        }
      } catch (err) {
        console.log('捕获错误', err)
      }
    },
  }
}
